import 'dart:convert';
import 'dart:io';
import 'package:http/http.dart' as http;
import 'package:opnvpn_flutter_example/server_config.dart';

var httpClient = HttpClient();
const String url = "developer.oneconnect.top";

Future<List<ServerConfig>> fetchServerConfig(bool free) async {
  // 表单数据
  var formData = {
    'package_name': 'com.appsuperx.vpnmyapp',
    'api_key': 'hve0NGiO8IZK17War5puiGeWI71BSoScjwF7YwO.5NyS8DFBKM',
    'encrypt': 'yes',
    'action': 'fetchUserServers',
    'type': free ? "free" : "pro"
  };

  var uri = Uri(scheme: 'https', host: url, path: "/view/front/controller.php");

  var response = await http.post(uri, body: formData);
  if (response.statusCode == 200) {
    List<dynamic> jsonList = jsonDecode(response.body);
    List<ServerConfig> serverConfigs =
        jsonList.map((json) => ServerConfig.fromJson(json)).toList();

    return serverConfigs;
  } else {
    return [];
  }
}
